package model

import (
	"gorm.io/gorm"
)

type DbTable struct {
	Schema     string `gorm:"column:schema"`
	Name       string `gorm:"column:name"`
	Comment    string `gorm:"column:comment"`
	CreateTime string `gorm:"column:createTime"`
}

type DbTableField struct {
	Name         string `gorm:"column:name"`
	ColumnType   string `gorm:"column:columnType"`
	Comment      string `gorm:"column:comment"`
	ColumnKey    string `gorm:"column:columnKey"`
	Extra        string `gorm:"column:extra"`
	IsNullable   string `gorm:"column:isNullable"`
	DefaultValue string `gorm:"column:defaultValue"`
}

type DbInfo struct {
}

func (d DbInfo) QueryTable(db *gorm.DB, database string) (dbTable []DbTable, err error) {
	querySql := "SELECT " +
		"TABLE_SCHEMA as `schema`," +
		"TABLE_NAME as `name`," +
		"TABLE_COMMENT as `comment`," +
		"CREATE_TIME as createTime " +
		"from information_schema.`TABLES` " +
		"WHERE TABLE_SCHEMA = ?"
	db.Raw(querySql, database).Scan(&dbTable)
	return
}

func (t DbTable) QueryField(db *gorm.DB) (dbTableField []DbTableField, err error) {
	querySql := "SELECT " +
		"COLUMN_NAME as `name`," +
		"COLUMN_TYPE as `columnType`," +
		"COLUMN_COMMENT as `comment`," +
		"COLUMN_KEY as `columnKey`," +
		"EXTRA as `extra`," +
		"IS_NULLABLE as `isNullable`," +
		"COLUMN_DEFAULT as `defaultValue` " +
		"from information_schema.`COLUMNS` " +
		"WHERE TABLE_SCHEMA = ? and table_name = ?"
	db.Raw(querySql, t.Schema, t.Name).Scan(&dbTableField)
	return
}
